package com.ymatou.iapi.reachpromotion.service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.ymatou.iapi.reachpromotion.parameter.Role;
import com.ymatou.iapi.reachpromotion.parameter.UserInfo;
import com.ymt.core.tool.Logger;

public class ReachPromitionVerify {

	public void verifyEqual(String commet, Object obj1, Object obj2) {
		try {
			Class class1 = obj1.getClass();
			Class class2 = obj1.getClass();
			
			Field[] fs1 = class1.getDeclaredFields();
			Field[] fs2 = class2.getDeclaredFields();

			if(fs1.length!=fs2.length)
			{
				System.out.println("传入类型属性长度不一致");
				return ;
			}
//			Logger.verifyEquals(fs1.length, fs2.length, commet + "传入类型属性长度不一致");

			// 如果有key属性的字段，先比较改字段
			for (Field field : fs1) {
				if (field.isAnnotationPresent(Role.class)) {
					Role role = (Role) field.getAnnotation(Role.class);
					if (role.value().equals("key")) {
//						Logger.verifyEquals(field.get(obj1), field.get(obj2), commet + ":" + field.getName() + "不一致");

//						if (field.getType().toString().endsWith("String")
//								&& (!field.get(obj1).toString().equals(field.get(obj2).toString())))
//							return;
//						else {
//							if (field.get(obj1) != field.get(obj2))
//								return;
//						}
						
						if(field.getType().toString().endsWith("int") || field.getType().toString().endsWith("Integer"))
						{
							String name = field.getName();
							name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
		                    Method m = obj1.getClass().getMethod("get" + name);
		                    Integer value1 = (Integer) m.invoke(obj1);
		                    Integer value2 = (Integer) m.invoke(obj2);
		                    
		                    if(value1!=value2)
		                    {
		                    	System.out.println(commet + ":" + name + "(key)不一致: "+value1.toString()+" | "+value2.toString());
		                    }
		                    return;
						}

					}
				}
			}

			for (int i = 0; i < fs1.length; i++) {
				Field f1 = fs1[i];
				Field f2 = fs2[i];

				// Logger.comment(message);

				if (f1.getType().toString().endsWith("int") || f1.getType().toString().endsWith("Integer")) {
					String name = f1.getName();
					name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
                    Method m = obj1.getClass().getMethod("get" + name);
                    Integer value1 = (Integer) m.invoke(obj1);
                    Integer value2 = (Integer) m.invoke(obj2);
                    
                    if(value1!=value2)
                    {
                    	System.out.println(commet + ":" + name + "不一致: "+value1.toString()+" | "+value2.toString());
                    }

//					Logger.verifyEquals(f1.get(obj1), f2.get(obj2), commet + ":" + name + "不一致");
				}

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return;
	}

	public boolean isRoleObject(Object obj1) {
		Class class1 = obj1.getClass();
		Field[] fs1 = class1.getDeclaredFields();
		for (Field field : fs1) {
			if (field.isAnnotationPresent(Role.class)) {
				Role role = (Role) field.getAnnotation(Role.class);
				if (role.value() == "key") {
					return true;
				}
			}
		}

		return false;
	}
	
	public boolean checkField(Field f1, Field f2, Object obj1,Object obj2,String commet)
	 throws Exception
	{
		String name = f1.getName();
		name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写，方便构造get，set方法
        Method m = obj1.getClass().getMethod("get" + name);
    
		if (f1.getType().toString().equals("class java.lang.Integer")) {
			Integer value1 = (Integer) m.invoke(obj1);
            Integer value2 = (Integer) m.invoke(obj2);
            
            if(value1!=value2)
            {
            	System.out.println(commet + ":" + name + "不一致: "+value1+" | "+value2);
            	return false;
            }
		}
		else if(f1.getType().toString().equals("class java.lang.String")){
			
			Integer value1 = (Integer) m.invoke(obj1);
            Integer value2 = (Integer) m.invoke(obj2);
            
            if(value1!=value2)
            {
            	System.out.println(commet + ":" + name + "不一致: "+value1.toString()+" | "+value2.toString());
            	return false;
            }
		}
		
		return true;
	}

	// public boolean checkKeyObject(String commet,Object obj1, Object obj2)
	// {
	// Class class1 = obj1.getClass();
	// Class class2 = obj1.getClass();
	//
	// Field[] fs1 = class1.getDeclaredFields();
	// Field[] fs2 = class2.getDeclaredFields();
	//
	//
	// }

	public static void main(String[] args) {
		Logger.comment("testS");
		UserInfo user1 = new UserInfo();
		UserInfo user2 = new UserInfo();
		
		user1.setId(1);
		user1.setName("testst1");
		user1.setSex(true);
		
		user2.setId(2);
		user2.setName("dfsgdfsgSDFG");
		user2.setSex(false);
		
		ReachPromitionVerify verify = new ReachPromitionVerify();
		verify.verifyEqual("test",user1,user2);
	}
}


